import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/pages/home/home_one_classify.dart';
import 'package:flutter_app/pages/home/info/home_one_information.dart';
import 'package:flutter_app/pages/home/house_one_recommend.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_app/widgets/common_swiper.dart';
import 'package:flutter_app/widgets/search_bar.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

/// @description: 首页

class HomeOne extends StatefulWidget {
  const HomeOne({Key? key}) : super(key: key);

  @override
  State<HomeOne> createState() => _HomeOneState();
}

class _HomeOneState extends State<HomeOne> {
  List<String> _imageList = [];
  List _recommendList = [];
  _getImageList() async {
    var res = await DioHttp.of(context).get('/home/swiper');
    var resString = json.decode(res.toString());
    var status = resString['status'];
    String desc = resString["description"] ?? "内部错误";
    if (status.toString().startsWith('2')) {
      var imageList = resString["body"];
      imageList.forEach((item) {
        setState(() {
          _imageList = _imageList..add(item["imgSrc"]);
        });
      });
    } else {
      EasyLoading.showError(desc);
    }
  }

  _getRecommend() async {
    var res = await DioHttp.of(context).get('/home/groups');
    var resString = json.decode(res.toString());
    var status = resString['status'];
    String desc = resString["description"] ?? "内部错误";
    if (status.toString().startsWith('2')) {
      var recommendList = resString["body"];
      setState(() {
        _recommendList = recommendList;
      });
      // print(_recommendList);
    } else {
      EasyLoading.showError(desc);
    }
  }

  @override
  void initState() {
    _getImageList();
    _getRecommend();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            backgroundColor: Colors.white,
            title: SearchBar(
              showMap: true,
              showLocation: true,
              onTabSearch: () {
                Navigator.of(context).pushNamed('/login');
              },
            )),
        body: ListView(
          children: [
            SwiperCom(
              images: _imageList,
            ),
            const SizedBox(
              height: 20,
            ),
            const HomeClassify(),
            HouseRecommend(
              recommendList: _recommendList,
            ),
            HoseInfomation()
          ],
        ));
  }
}
